﻿

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace StudentManagement
{
    public partial class Form_teacher : Form
    {

        public String formerID, teacherName;
        public Form_teacher(String name)
        {

            InitializeComponent();
            this.teacherName = name;
            showTeacherInfo();
            showClassInfo();
            showStudentList();
            label1.Text = textBox3.Text + "老师，您好！";


        }

        //连接数据库
        public MySqlConnection connectionF()
        {
            String connStr = "server=localhost;user id=root;Password=wutong;persist security info=True;database=sms";
            MySqlConnection conn = new MySqlConnection(connStr);
            try
            {
                conn.Open();
            }
            catch (System.Exception e)
            {
                MessageBox.Show("无法打开数据库！\n" + e.Message, "错误提示",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                return null;
            }
            return conn;
        }



        /********教师个人信息界面***********/

        //显示教师号、姓名、办公室、电话、个人简介
        private void showTeacherInfo()
        {

            MySqlConnection conn = connectionF();
            String cmdStr = "select * from teacher_info where ID='" + teacherName + "'";
            MySqlCommand cmd = new MySqlCommand(cmdStr, conn);
            MySqlDataReader dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                textBox1.Text = teacherName;
                textBox3.Text = dr["name"].ToString();
                textBox4.Text = dr["office"].ToString();
                textBox5.Text = dr["tele_no"].ToString();
                richTextBox1.Text = dr["description"].ToString();
            }
            else
                MessageBox.Show("打开个人信息错误！", "错误",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
            if (!dr.IsClosed)
                dr.Close();
            if (conn.State.Equals(ConnectionState.Open))
                conn.Close();

        }


        /********学生成绩评定***********/


        //显示学生信息

        private void showClassInfo()
        {

            MySqlConnection conn = connectionF();
            String cmdStr = "select * from lesson_teach where ID='" + teacherName + "'";
            MySqlCommand cmd = new MySqlCommand(cmdStr, conn);
            MySqlDataReader dr = cmd.ExecuteReader();

            if (dr.Read())
            {
                textBox6.Text = dr["lessonID"].ToString();
                textBox9.Text = dr["s_time"].ToString() + "-" + dr["o_time"].ToString();
                textBox10.Text = dr["classroom"].ToString();
                textBox8.Text = dr["week"].ToString();

                dr.Close();
                String cmdStr2 = "select * from lesson where ID='" + textBox6.Text + "'";
                MySqlCommand cmd2 = new MySqlCommand(cmdStr2, conn);
                MySqlDataReader dr2 = cmd2.ExecuteReader();
                if (dr2.Read())
                {
                    textBox7.Text = dr2["name"].ToString();
                }
                else
                {
                    textBox7.ResetText();
                }
                if (!dr2.IsClosed)
                {
                    dr2.Close();
                }


                String cmdStr4 = "select count(*) as c from lesson_choice where lesson_ID='" + textBox6.Text + "'";
                MySqlCommand cmd4 = new MySqlCommand(cmdStr4, conn);
                MySqlDataReader dr4 = cmd4.ExecuteReader();
                if (dr4.Read())
                {
                    textBox11.Text = dr4["c"].ToString();

                }
                else
                {
                    textBox1.Text = "0";
                }
                dr4.Close();


            }

            else
                MessageBox.Show("打开教师详细信息失败，请重试！", "错误",
                                 MessageBoxButtons.OK, MessageBoxIcon.Error);
            if (!dr.IsClosed)
            {
                dr.Close();
            }

            if (conn.State.Equals(ConnectionState.Open))
                conn.Close();


        }

        //将学生的学号添加到修改界面中
        private void showStudentList()
        {
            String cmdStr = "select sno from lesson_choice where lesson_ID='" + textBox6.Text + "'";
            MySqlConnection conn = connectionF();
            MySqlCommand cmd = new MySqlCommand(cmdStr, conn);
            MySqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
                listBox1.Items.Add(dr["sno"]).ToString();
            if (!dr.IsClosed)
                dr.Close();
            if (conn.State.Equals(ConnectionState.Open))
                conn.Close();
        }


        //将被选中的学生学号、学生姓名显示出来
        private void listBox1_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            textBox2.Text = listBox1.SelectedItem.ToString();
            MySqlConnection conn = connectionF();
            String cmdStr = "select * from student where ID='" + textBox2.Text + "'";
            MySqlCommand cmd = new MySqlCommand(cmdStr, conn);
            MySqlDataReader dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                textBox12.Text = dr["name"].ToString();

                dr.Close();

                String cmdStr1 = "select * from lesson_choice where sno='" + textBox2.Text + "'";
                MySqlCommand cmd1 = new MySqlCommand(cmdStr1, conn);
                MySqlDataReader dr1 = cmd1.ExecuteReader();

                if (dr1.Read())
                {
                    if (!dr1["score"].ToString().Equals("-1"))
                        textBox13.Text = dr1["score"].ToString();
                    else
                        textBox13.Text = "未评定";
                }
                dr1.Close();
            }

            else
                MessageBox.Show("打开学生详细信息失败，请重试！", "错误",
                        MessageBoxButtons.OK, MessageBoxIcon.Error);
            if (!dr.IsClosed)
                dr.Close();
            if (conn.State.Equals(ConnectionState.Open))
                conn.Close();
            formerID = textBox1.Text.ToString();
        }

        //给定学生成绩
        private void button3_Click_1(object sender, EventArgs e)
        {

            if (textBox2.Text == "" || textBox2.Text.Length == 0)
            {
                MessageBox.Show("未选择要给定成绩的学生，请重试！", "错误",
                      MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (!checkGrade(textBox13))
            {
                MessageBox.Show("请输入正确的成绩！","提示",MessageBoxButtons.OK,MessageBoxIcon.Error,MessageBoxDefaultButton.Button1);
                return;
            }
            String score = textBox13.Text;
            String cmdStr = "update lesson_choice set score='" +
                score + "' where sno='" +
                textBox2.Text + "'";

            MySqlConnection conn = connectionF();
            MySqlCommand cmd = new MySqlCommand(cmdStr, conn);

            if (cmd.ExecuteNonQuery() != 0)
                MessageBox.Show("学生成绩评定成功！请点击继续···", "提示",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
            else
                MessageBox.Show("学生成绩评定失败,请重试！", "错误",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);

            if (conn.State.Equals(ConnectionState.Open))
                conn.Close();
            listBox1.Items.Clear();
            showStudentList();
        }


        //判断是否符合成绩的输入规定
        private Boolean checkGrade(TextBox tb)
        {
            int tmp=-1;
            String tbS = tb.Text;
            try
            {
                tmp = Convert.ToInt32(tbS);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
            return (tmp <= 100 && tmp >= 0);        
        }


        //修改密码
        private void button2_Click_1(object sender, EventArgs e)
        {
            Form_chPassword changeP = new Form_chPassword(teacherName, "teacher_info");
            changeP.Show();
        }
//修改个人信息
        private void button1_Click(object sender, EventArgs e)
        {


            MySqlConnection conn = connectionF();
            String cmdStr = "update teacher_info set tele_no='" +
                textBox5.Text + "', office='" +
                textBox4.Text + "' where ID='" +
                teacherName + "'";
            MySqlCommand cmd = new MySqlCommand(cmdStr, conn);
            if (cmd.ExecuteNonQuery() == 1)
                MessageBox.Show("信息修改成功！请点击继续···", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
            else
                MessageBox.Show("修改个人信息错误！", "错误",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);

            if (conn.State.Equals(ConnectionState.Open))
                conn.Close();


        }        
        
    }
}